﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using GameSPK.DAO;

namespace GameSPK.Helpers
{
    class MultiEventHelp
    {
        public static MultiEvent getMultiEventFromXML(string filename)
        {
            string fileXML = filename;
            MultiEvent multievent = new MultiEvent();
            if (File.Exists(fileXML))
            {
                XElement layout = XElement.Load(fileXML);
                var home = (from c in layout.Elements()
                            where c.Name == "MainEvent"
                            select c).Single();
                multievent.chuDe = ChuDeDAO.getChuDe(home.Attribute("ChuDe").Value);
                multievent.cauhoi = home.Attribute("CauHoi").Value;
                multievent.dapan = home.Attribute("DapAn").Value;
                multievent.sodukien = int.Parse(home.Attribute("SoEvent").Value);
                string tabname ;
                for (int i = 0; i < multievent.sodukien; i++)
                {
                    tabname = "Event" +i;
                    home = (from c in layout.Element("Events").Elements()
                                where c.Name ==  tabname
                                select c).Single();
                    CEvent cEvent = new CEvent();
                    cEvent.NoiDung = home.Attribute("NoiDung").Value;
                    cEvent.eloaiDuKien = (loaidukien)Enum.Parse(typeof(loaidukien), home.Attribute("LoaiEvent").Value);
                    multievent[i] = cEvent;
                }
                return multievent;
            }
            return null;
        }
    }
}
